home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Getopt / Long.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  31.1 KB  |  1,119 lines

  1.  
  2. package Getopt::Long;
  3.  
  4.  
  5. =head1 NAME
  6.  
  7. GetOptions - extended processing of command line options
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.   use Getopt::Long;
  12.   $result = GetOptions (...option-descriptions...);
  13.  
  14. =head1 DESCRIPTION
  15.  
  16. The Getopt::Long module implements an extended getopt function called
  17. GetOptions(). This function adheres to the POSIX syntax for command
  18. line options, with GNU extensions. In general, this means that options
  19. have long names instead of single letters, and are introduced with a
  20. double dash "--". Support for bundling of command line options, as was
  21. the case with the more traditional single-letter approach, is provided
  22. but not enabled by default. For example, the UNIX "ps" command can be
  23. given the command line "option"
  24.  
  25.   -vax
  26.  
  27. which means the combination of B<-v>, B<-a> and B<-x>. With the new
  28. syntax B<--vax> would be a single option, probably indicating a
  29. computer architecture. 
  30.  
  31. Command line options can be used to set values. These values can be
  32. specified in one of two ways:
  33.  
  34.   --size 24
  35.   --size=24
  36.  
  37. GetOptions is called with a list of option-descriptions, each of which
  38. consists of two elements: the option specifier and the option linkage.
  39. The option specifier defines the name of the option and, optionally,
  40. the value it can take. The option linkage is usually a reference to a
  41. variable that will be set when the option is used. For example, the
  42. following call to GetOptions:
  43.  
  44.   GetOptions("size=i" => \$offset);
  45.  
  46. will accept a command line option "size" that must have an integer
  47. value. With a command line of "--size 24" this will cause the variable
  48. $offset to get the value 24.
  49.  
  50. Alternatively, the first argument to GetOptions may be a reference to
  51. a HASH describing the linkage for the options. The following call is
  52. equivalent to the example above:
  53.  
  54.   %optctl = ("size" => \$offset);
  55.   GetOptions(\%optctl, "size=i");
  56.  
  57. Linkage may be specified using either of the above methods, or both.
  58. Linkage specified in the argument list takes precedence over the
  59. linkage specified in the HASH.
  60.  
  61. The command line options are taken from array @ARGV. Upon completion
  62. of GetOptions, @ARGV will contain the rest (i.e. the non-options) of
  63. the command line.
  64.  
  65. Each option specifier designates the name of the option, optionally
  66. followed by an argument specifier. Values for argument specifiers are:
  67.  
  68. =over 8
  69.  
  70. =item E<lt>noneE<gt>
  71.  
  72. Option does not take an argument. 
  73. The option variable will be set to 1.
  74.  
  75. =item !
  76.  
  77. Option does not take an argument and may be negated, i.e. prefixed by
  78. "no". E.g. "foo!" will allow B<--foo> (with value 1) and B<-nofoo>
  79. (with value 0).
  80. The option variable will be set to 1, or 0 if negated.
  81.  
  82. =item =s
  83.  
  84. Option takes a mandatory string argument.
  85. This string will be assigned to the option variable.
  86. Note that even if the string argument starts with B<-> or B<-->, it
  87. will not be considered an option on itself.
  88.  
  89. =item :s
  90.  
  91. Option takes an optional string argument.
  92. This string will be assigned to the option variable.
  93. If omitted, it will be assigned "" (an empty string).
  94. If the string argument starts with B<-> or B<-->, it
  95. will be considered an option on itself.
  96.  
  97. =item =i
  98.  
  99. Option takes a mandatory integer argument.
  100. This value will be assigned to the option variable.
  101. Note that the value may start with B<-> to indicate a negative
  102. value. 
  103.  
  104. =item :i
  105.  
  106. Option takes an optional integer argument.
  107. This value will be assigned to the option variable.
  108. If omitted, the value 0 will be assigned.
  109. Note that the value may start with B<-> to indicate a negative
  110. value.
  111.  
  112. =item =f
  113.  
  114. Option takes a mandatory real number argument.
  115. This value will be assigned to the option variable.
  116. Note that the value may start with B<-> to indicate a negative
  117. value.
  118.  
  119. =item :f
  120.  
  121. Option takes an optional real number argument.
  122. This value will be assigned to the option variable.
  123. If omitted, the value 0 will be assigned.
  124.  
  125. =back
  126.  
  127. A lone dash B<-> is considered an option, the corresponding option
  128. name is the empty string.
  129.  
  130. A double dash on itself B<--> signals end of the options list.
  131.  
  132. =head2 Linkage specification
  133.  
  134. The linkage specifier is optional. If no linkage is explicitly
  135. specified but a ref HASH is passed, GetOptions will place the value in
  136. the HASH. For example:
  137.  
  138.   %optctl = ();
  139.   GetOptions (\%optctl, "size=i");
  140.  
  141. will perform the equivalent of the assignment
  142.  
  143.   $optctl{"size"} = 24;
  144.  
  145. For array options, a reference to an array is used, e.g.:
  146.  
  147.   %optctl = ();
  148.   GetOptions (\%optctl, "sizes=i@");
  149.  
  150. with command line "-sizes 24 -sizes 48" will perform the equivalent of
  151. the assignment
  152.  
  153.   $optctl{"sizes"} = [24, 48];
  154.  
  155. For hash options (an option whose argument looks like "name=value"),
  156. a reference to a hash is used, e.g.:
  157.  
  158.   %optctl = ();
  159.   GetOptions (\%optctl, "define=s%");
  160.  
  161. with command line "--define foo=hello --define bar=world" will perform the
  162. equivalent of the assignment
  163.  
  164.   $optctl{"define"} = {foo=>'hello', bar=>'world')
  165.  
  166. If no linkage is explicitly specified and no ref HASH is passed,
  167. GetOptions will put the value in a global variable named after the
  168. option, prefixed by "opt_". To yield a usable Perl variable,
  169. characters that are not part of the syntax for variables are
  170. translated to underscores. For example, "--fpp-struct-return" will set
  171. the variable $opt_fpp_struct_return. Note that this variable resides
  172. in the namespace of the calling program, not necessarily B<main>.
  173. For example:
  174.  
  175.   GetOptions ("size=i", "sizes=i@");
  176.  
  177. with command line "-size 10 -sizes 24 -sizes 48" will perform the
  178. equivalent of the assignments
  179.  
  180.   $opt_size = 10;
  181.   @opt_sizes = (24, 48);
  182.  
  183. A lone dash B<-> is considered an option, the corresponding Perl
  184. identifier is $opt_ .
  185.  
  186. The linkage specifier can be a reference to a scalar, a reference to
  187. an array, a reference to a hash or a reference to a subroutine.
  188.  
  189. If a REF SCALAR is supplied, the new value is stored in the referenced
  190. variable. If the option occurs more than once, the previous value is
  191. overwritten. 
  192.  
  193. If a REF ARRAY is supplied, the new value is appended (pushed) to the
  194. referenced array. 
  195.  
  196. If a REF HASH is supplied, the option value should look like "key" or
  197. "key=value" (if the "=value" is omitted then a value of 1 is implied).
  198. In this case, the element of the referenced hash with the key "key"
  199. is assigned "value". 
  200.  
  201. If a REF CODE is supplied, the referenced subroutine is called with
  202. two arguments: the option name and the option value.
  203. The option name is always the true name, not an abbreviation or alias.
  204.  
  205. =head2 Aliases and abbreviations
  206.  
  207. The option name may actually be a list of option names, separated by
  208. "|"s, e.g. "foo|bar|blech=s". In this example, "foo" is the true name
  209. of this option. If no linkage is specified, options "foo", "bar" and
  210. "blech" all will set $opt_foo.
  211.  
  212. Option names may be abbreviated to uniqueness, depending on
  213. configuration option B<auto_abbrev>.
  214.  
  215. =head2 Non-option call-back routine
  216.  
  217. A special option specifier, E<lt>E<gt>, can be used to designate a subroutine
  218. to handle non-option arguments. GetOptions will immediately call this
  219. subroutine for every non-option it encounters in the options list.
  220. This subroutine gets the name of the non-option passed.
  221. This feature requires configuration option B<permute>, see section
  222. CONFIGURATION OPTIONS.
  223.  
  224. See also the examples.
  225.  
  226. =head2 Option starters
  227.  
  228. On the command line, options can start with B<-> (traditional), B<-->
  229. (POSIX) and B<+> (GNU, now being phased out). The latter is not
  230. allowed if the environment variable B<POSIXLY_CORRECT> has been
  231. defined.
  232.  
  233. Options that start with "--" may have an argument appended, separated
  234. with an "=", e.g. "--foo=bar".
  235.  
  236. =head2 Return value
  237.  
  238. A return status of 0 (false) indicates that the function detected
  239. one or more errors.
  240.  
  241. =head1 COMPATIBILITY
  242.  
  243. Getopt::Long::GetOptions() is the successor of
  244. B<newgetopt.pl> that came with Perl 4. It is fully upward compatible.
  245. In fact, the Perl 5 version of newgetopt.pl is just a wrapper around
  246. the module.
  247.  
  248. If an "@" sign is appended to the argument specifier, the option is
  249. treated as an array. Value(s) are not set, but pushed into array
  250. @opt_name. If explicit linkage is supplied, this must be a reference
  251. to an ARRAY.
  252.  
  253. If an "%" sign is appended to the argument specifier, the option is
  254. treated as a hash. Value(s) of the form "name=value" are set by
  255. setting the element of the hash %opt_name with key "name" to "value"
  256. (if the "=value" portion is omitted it defaults to 1). If explicit
  257. linkage is supplied, this must be a reference to a HASH.
  258.  
  259. If configuration option B<getopt_compat> is set (see section
  260. CONFIGURATION OPTIONS), options that start with "+" or "-" may also
  261. include their arguments, e.g. "+foo=bar". This is for compatiblity
  262. with older implementations of the GNU "getopt" routine.
  263.  
  264. If the first argument to GetOptions is a string consisting of only
  265. non-alphanumeric characters, it is taken to specify the option starter
  266. characters. Everything starting with one of these characters from the
  267. starter will be considered an option. B<Using a starter argument is
  268. strongly deprecated.>
  269.  
  270. For convenience, option specifiers may have a leading B<-> or B<-->,
  271. so it is possible to write:
  272.  
  273.    GetOptions qw(-foo=s --bar=i --ar=s);
  274.  
  275. =head1 EXAMPLES
  276.  
  277. If the option specifier is "one:i" (i.e. takes an optional integer
  278. argument), then the following situations are handled:
  279.  
  280.    -one -two        -> $opt_one = '', -two is next option
  281.    -one -2        -> $opt_one = -2
  282.  
  283. Also, assume specifiers "foo=s" and "bar:s" :
  284.  
  285.    -bar -xxx        -> $opt_bar = '', '-xxx' is next option
  286.    -foo -bar        -> $opt_foo = '-bar'
  287.    -foo --        -> $opt_foo = '--'
  288.  
  289. In GNU or POSIX format, option names and values can be combined:
  290.  
  291.    +foo=blech        -> $opt_foo = 'blech'
  292.    --bar=        -> $opt_bar = ''
  293.    --bar=--        -> $opt_bar = '--'
  294.  
  295. Example of using variable references:
  296.  
  297.    $ret = GetOptions ('foo=s', \$foo, 'bar=i', 'ar=s', \@ar);
  298.  
  299. With command line options "-foo blech -bar 24 -ar xx -ar yy" 
  300. this will result in:
  301.  
  302.    $foo = 'blech'
  303.    $opt_bar = 24
  304.    @ar = ('xx','yy')
  305.  
  306. Example of using the E<lt>E<gt> option specifier:
  307.  
  308.    @ARGV = qw(-foo 1 bar -foo 2 blech);
  309.    GetOptions("foo=i", \$myfoo, "<>", \&mysub);
  310.  
  311. Results:
  312.  
  313.    mysub("bar") will be called (with $myfoo being 1)
  314.    mysub("blech") will be called (with $myfoo being 2)
  315.  
  316. Compare this with:
  317.  
  318.    @ARGV = qw(-foo 1 bar -foo 2 blech);
  319.    GetOptions("foo=i", \$myfoo);
  320.  
  321. This will leave the non-options in @ARGV:
  322.  
  323.    $myfoo -> 2
  324.    @ARGV -> qw(bar blech)
  325.  
  326. =head1 CONFIGURATION OPTIONS
  327.  
  328. B<GetOptions> can be configured by calling subroutine
  329. B<Getopt::Long::config>. This subroutine takes a list of quoted
  330. strings, each specifying a configuration option to be set, e.g.
  331. B<ignore_case>. Options can be reset by prefixing with B<no_>, e.g.
  332. B<no_ignore_case>. Case does not matter. Multiple calls to B<config>
  333. are possible.
  334.  
  335. Previous versions of Getopt::Long used variables for the purpose of
  336. configuring. Although manipulating these variables still work, it
  337. is strongly encouraged to use the new B<config> routine. Besides, it
  338. is much easier.
  339.  
  340. The following options are available:
  341.  
  342. =over 12
  343.  
  344. =item default
  345.  
  346. This option causes all configuration options to be reset to their
  347. default values.
  348.  
  349. =item auto_abbrev
  350.  
  351. Allow option names to be abbreviated to uniqueness.
  352. Default is set unless environment variable
  353. POSIXLY_CORRECT has been set, in which case B<auto_abbrev> is reset.
  354.  
  355. =item getopt_compat   
  356.  
  357. Allow '+' to start options.
  358. Default is set unless environment variable
  359. POSIXLY_CORRECT has been set, in which case B<getopt_compat> is reset.
  360.  
  361. =item require_order
  362.  
  363. Whether non-options are allowed to be mixed with
  364. options.
  365. Default is set unless environment variable
  366. POSIXLY_CORRECT has been set, in which case b<require_order> is reset.
  367.  
  368. See also B<permute>, which is the opposite of B<require_order>.
  369.  
  370. =item permute
  371.  
  372. Whether non-options are allowed to be mixed with
  373. options.
  374. Default is set unless environment variable
  375. POSIXLY_CORRECT has been set, in which case B<permute> is reset.
  376. Note that B<permute> is the opposite of B<require_order>.
  377.  
  378. If B<permute> is set, this means that 
  379.  
  380.     -foo arg1 -bar arg2 arg3
  381.  
  382. is equivalent to
  383.  
  384.     -foo -bar arg1 arg2 arg3
  385.  
  386. If a non-option call-back routine is specified, @ARGV will always be
  387. empty upon succesful return of GetOptions since all options have been
  388. processed, except when B<--> is used:
  389.  
  390.     -foo arg1 -bar arg2 -- arg3
  391.  
  392. will call the call-back routine for arg1 and arg2, and terminate
  393. leaving arg2 in @ARGV.
  394.  
  395. If B<require_order> is set, options processing
  396. terminates when the first non-option is encountered.
  397.  
  398.     -foo arg1 -bar arg2 arg3
  399.  
  400. is equivalent to
  401.  
  402.     -foo -- arg1 -bar arg2 arg3
  403.  
  404. =item bundling (default: reset)
  405.  
  406. Setting this variable to a non-zero value will allow single-character
  407. options to be bundled. To distinguish bundles from long option names,
  408. long options must be introduced with B<--> and single-character
  409. options (and bundles) with B<->. For example,
  410.  
  411.     ps -vax --vax
  412.  
  413. would be equivalent to
  414.  
  415.     ps -v -a -x --vax
  416.  
  417. provided "vax", "v", "a" and "x" have been defined to be valid
  418. options. 
  419.  
  420. Bundled options can also include a value in the bundle; this value has
  421. to be the last part of the bundle, e.g.
  422.  
  423.     scale -h24 -w80
  424.  
  425. is equivalent to
  426.  
  427.     scale -h 24 -w 80
  428.  
  429. Note: resetting B<bundling> also resets B<bundling_override>.
  430.  
  431. =item bundling_override (default: reset)
  432.  
  433. If B<bundling_override> is set, bundling is enabled as with
  434. B<bundling> but now long option names override option bundles. In the
  435. above example, B<-vax> would be interpreted as the option "vax", not
  436. the bundle "v", "a", "x".
  437.  
  438. Note: resetting B<bundling_override> also resets B<bundling>.
  439.  
  440. B<Note:> Using option bundling can easily lead to unexpected results,
  441. especially when mixing long options and bundles. Caveat emptor.
  442.  
  443. =item ignore_case  (default: set)
  444.  
  445. If set, case is ignored when matching options.
  446.  
  447. Note: resetting B<ignore_case> also resets B<ignore_case_always>.
  448.  
  449. =item ignore_case_always (default: reset)
  450.  
  451. When bundling is in effect, case is ignored on single-character
  452. options also. 
  453.  
  454. Note: resetting B<ignore_case_always> also resets B<ignore_case>.
  455.  
  456. =item pass_through (default: reset)
  457.  
  458. Unknown options are passed through in @ARGV instead of being flagged
  459. as errors. This makes it possible to write wrapper scripts that
  460. process only part of the user supplied options, and passes the
  461. remaining options to some other program.
  462.  
  463. This can be very confusing, especially when B<permute> is also set.
  464.  
  465. =item debug (default: reset)
  466.  
  467. Enable copious debugging output.
  468.  
  469. =back
  470.  
  471. =head1 OTHER USEFUL VARIABLES
  472.  
  473. =over 12
  474.  
  475. =item $Getopt::Long::VERSION
  476.  
  477. The version number of this Getopt::Long implementation in the format
  478. C<major>.C<minor>. This can be used to have Exporter check the
  479. version, e.g.
  480.  
  481.     use Getopt::Long 3.00;
  482.  
  483. You can inspect $Getopt::Long::major_version and
  484. $Getopt::Long::minor_version for the individual components.
  485.  
  486. =item $Getopt::Long::error
  487.  
  488. Internal error flag. May be incremented from a call-back routine to
  489. cause options parsing to fail.
  490.  
  491. =back
  492.  
  493. =cut
  494.  
  495.  
  496.  
  497.  
  498. use strict;
  499.  
  500. BEGIN {
  501.     require 5.003;
  502.     use Exporter ();
  503.     use vars   qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  504.     $VERSION   = sprintf("%d.%02d", q$Revision: 2.10 $ =~ /(\d+)\.(\d+)/);
  505.  
  506.     @ISA       = qw(Exporter);
  507.     @EXPORT    = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER);
  508.     %EXPORT_TAGS = ();
  509.     @EXPORT_OK = qw();
  510. }
  511.  
  512. use vars @EXPORT, @EXPORT_OK;
  513. use vars qw($error $debug $major_version $minor_version);
  514. use vars qw($autoabbrev $getopt_compat $ignorecase $bundling $order
  515.         $passthrough);
  516.  
  517.  
  518. my $gen_prefix;            # generic prefix (option starters)
  519. my $argend;            # option list terminator
  520. my %opctl;            # table of arg.specs (long and abbrevs)
  521. my %bopctl;            # table of arg.specs (bundles)
  522. my @opctl;            # the possible long option names
  523. my $pkg;            # current context. Needed if no linkage.
  524. my %aliases;            # alias table
  525. my $genprefix;            # so we can call the same module more 
  526. my $opt;            # current option
  527. my $arg;            # current option value, if any
  528. my $array;            # current option is array typed
  529. my $hash;            # current option is hash typed
  530. my $key;            # hash key for a hash option
  531. my $config_defaults;        # set config defaults
  532. my $find_option;        # helper routine
  533.  
  534.  
  535. sub GetOptions {
  536.  
  537.     my @optionlist = @_;    # local copy of the option descriptions
  538.     $argend = '--';        # option list terminator
  539.     %opctl = ();        # table of arg.specs (long and abbrevs)
  540.     %bopctl = ();        # table of arg.specs (bundles)
  541.     $pkg = (caller)[0];        # current context
  542.     %aliases= ();        # alias table
  543.     my @ret = ();        # accum for non-options
  544.     my %linkage;        # linkage
  545.     my $userlinkage;        # user supplied HASH
  546.     $genprefix = $gen_prefix;    # so we can call the same module many times
  547.     $error = 0;
  548.  
  549.     print STDERR ('GetOptions $Revision: 2.10 $ ',
  550.           "[GetOpt::Long $Getopt::Long::VERSION] -- ",
  551.           "called from package \"$pkg\".\n",
  552.           "  (@ARGV)\n",
  553.           "  autoabbrev=$autoabbrev".
  554.           ",bundling=$bundling",
  555.           ",getopt_compat=$getopt_compat",
  556.           ",order=$order",
  557.           ",\n  ignorecase=$ignorecase",
  558.           ",passthrough=$passthrough",
  559.           ",genprefix=\"$genprefix\"",
  560.           ".\n")
  561.     if $debug;
  562.  
  563.     $userlinkage = undef;
  564.     if ( ref($optionlist[0]) && ref($optionlist[0]) eq 'HASH' ) {
  565.     $userlinkage = shift (@optionlist);
  566.     }
  567.  
  568.     if ( $optionlist[0] =~ /^\W+$/ ) {
  569.     $genprefix = shift (@optionlist);
  570.     $genprefix =~ s/(\W)/\\$1/g;
  571.     $genprefix = "[" . $genprefix . "]";
  572.     }
  573.  
  574.     %opctl = ();
  575.     %bopctl = ();
  576.     while ( @optionlist > 0 ) {
  577.     my $opt = shift (@optionlist);
  578.  
  579.     $opt = $' if $opt =~ /^($genprefix)+/;
  580.  
  581.     if ( $opt eq '<>' ) {
  582.         if ( (defined $userlinkage)
  583.         && !(@optionlist > 0 && ref($optionlist[0]))
  584.         && (exists $userlinkage->{$opt})
  585.         && ref($userlinkage->{$opt}) ) {
  586.         unshift (@optionlist, $userlinkage->{$opt});
  587.         }
  588.         unless ( @optionlist > 0 
  589.             && ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) {
  590.         warn ("Option spec <> requires a reference to a subroutine\n");
  591.         $error++;
  592.         next;
  593.         }
  594.         $linkage{'<>'} = shift (@optionlist);
  595.         next;
  596.     }
  597.  
  598.     if ( $opt !~ /^(\w+[-\w|]*)?(!|[=:][infse][@%]?)?$/ ) {
  599.         warn ("Error in option spec: \"", $opt, "\"\n");
  600.         $error++;
  601.         next;
  602.     }
  603.     my ($o, $c, $a) = ($1, $2);
  604.     $c = '' unless defined $c;
  605.  
  606.     if ( ! defined $o ) {
  607.         $opctl{$o = ''} = $c;
  608.     }
  609.     else {
  610.         my @o =  split (/\|/, $o);
  611.         my $linko = $o = $o[0];
  612.         $a = $o unless $o eq lc($o);
  613.         $o = lc ($o)
  614.         if $ignorecase > 1 
  615.             || ($ignorecase
  616.             && ($bundling ? length($o) > 1  : 1));
  617.  
  618.         foreach ( @o ) {
  619.         if ( $bundling && length($_) == 1 ) {
  620.             $_ = lc ($_) if $ignorecase > 1;
  621.             if ( $c eq '!' ) {
  622.             $opctl{"no$_"} = $c;
  623.             warn ("Ignoring '!' modifier for short option $_\n");
  624.             $c = '';
  625.             }
  626.             $opctl{$_} = $bopctl{$_} = $c;
  627.         }
  628.         else {
  629.             $_ = lc ($_) if $ignorecase;
  630.             if ( $c eq '!' ) {
  631.             $opctl{"no$_"} = $c;
  632.             $c = '';
  633.             }
  634.             $opctl{$_} = $c;
  635.         }
  636.         if ( defined $a ) {
  637.             $aliases{$_} = $a;
  638.         }
  639.         else {
  640.             $a = $_;
  641.         }
  642.         }
  643.         $o = $linko;
  644.     }
  645.  
  646.     if ( defined $userlinkage ) {
  647.         unless ( @optionlist > 0 && ref($optionlist[0]) ) {
  648.         if ( exists $userlinkage->{$o} && ref($userlinkage->{$o}) ) {
  649.             print STDERR ("=> found userlinkage for \"$o\": ",
  650.                   "$userlinkage->{$o}\n")
  651.             if $debug;
  652.             unshift (@optionlist, $userlinkage->{$o});
  653.         }
  654.         else {
  655.             next;
  656.         }
  657.         }
  658.     }
  659.  
  660.     if ( @optionlist > 0 && ref($optionlist[0]) ) {
  661.         print STDERR ("=> link \"$o\" to $optionlist[0]\n")
  662.         if $debug;
  663.         if ( ref($optionlist[0]) =~ /^(SCALAR|CODE)$/ ) {
  664.         $linkage{$o} = shift (@optionlist);
  665.         }
  666.         elsif ( ref($optionlist[0]) =~ /^(ARRAY)$/ ) {
  667.         $linkage{$o} = shift (@optionlist);
  668.         $opctl{$o} .= '@'
  669.           if $opctl{$o} ne '' and $opctl{$o} !~ /\@$/;
  670.         $bopctl{$o} .= '@'
  671.           if $bundling and $bopctl{$o} ne '' and $bopctl{$o} !~ /\@$/;
  672.         }
  673.         elsif ( ref($optionlist[0]) =~ /^(HASH)$/ ) {
  674.         $linkage{$o} = shift (@optionlist);
  675.         $opctl{$o} .= '%'
  676.           if $opctl{$o} ne '' and $opctl{$o} !~ /\%$/;
  677.         $bopctl{$o} .= '%'
  678.           if $bundling and $bopctl{$o} ne '' and $bopctl{$o} !~ /\%$/;
  679.         }
  680.         else {
  681.         warn ("Invalid option linkage for \"", $opt, "\"\n");
  682.         $error++;
  683.         }
  684.     }
  685.     else {
  686.         my $ov = $o;
  687.         $ov =~ s/\W/_/g;
  688.         if ( $c =~ /@/ ) {
  689.         print STDERR ("=> link \"$o\" to \@$pkg","::opt_$ov\n")
  690.             if $debug;
  691.         eval ("\$linkage{\$o} = \\\@".$pkg."::opt_$ov;");
  692.         }
  693.         elsif ( $c =~ /%/ ) {
  694.         print STDERR ("=> link \"$o\" to \%$pkg","::opt_$ov\n")
  695.             if $debug;
  696.         eval ("\$linkage{\$o} = \\\%".$pkg."::opt_$ov;");
  697.         }
  698.         else {
  699.         print STDERR ("=> link \"$o\" to \$$pkg","::opt_$ov\n")
  700.             if $debug;
  701.         eval ("\$linkage{\$o} = \\\$".$pkg."::opt_$ov;");
  702.         }
  703.     }
  704.     }
  705.  
  706.     return 0 if $error;
  707.  
  708.     @opctl = sort(keys (%opctl)) if $autoabbrev;
  709.  
  710.     if ( $debug ) {
  711.     my ($arrow, $k, $v);
  712.     $arrow = "=> ";
  713.     while ( ($k,$v) = each(%opctl) ) {
  714.         print STDERR ($arrow, "\$opctl{\"$k\"} = \"$v\"\n");
  715.         $arrow = "   ";
  716.     }
  717.     $arrow = "=> ";
  718.     while ( ($k,$v) = each(%bopctl) ) {
  719.         print STDERR ($arrow, "\$bopctl{\"$k\"} = \"$v\"\n");
  720.         $arrow = "   ";
  721.     }
  722.     }
  723.  
  724.     while ( @ARGV > 0 ) {
  725.  
  726.  
  727.     $opt = shift (@ARGV);
  728.     $arg = undef;
  729.     $array = $hash = 0;
  730.     print STDERR ("=> option \"", $opt, "\"\n") if $debug;
  731.  
  732.  
  733.     if ( $opt eq $argend ) {
  734.         unshift (@ARGV, @ret) 
  735.         if $order == $PERMUTE;
  736.         return ($error == 0);
  737.     }
  738.  
  739.     my $tryopt = $opt;
  740.  
  741.     if ( &$find_option () ) {
  742.         
  743.         next unless defined $opt;
  744.  
  745.         if ( defined $arg ) {
  746.         $opt = $aliases{$opt} if defined $aliases{$opt};
  747.  
  748.         if ( defined $linkage{$opt} ) {
  749.             print STDERR ("=> ref(\$L{$opt}) -> ",
  750.                   ref($linkage{$opt}), "\n") if $debug;
  751.  
  752.             if ( ref($linkage{$opt}) eq 'SCALAR' ) {
  753.             print STDERR ("=> \$\$L{$opt} = \"$arg\"\n") if $debug;
  754.             ${$linkage{$opt}} = $arg;
  755.             }
  756.             elsif ( ref($linkage{$opt}) eq 'ARRAY' ) {
  757.             print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n")
  758.                 if $debug;
  759.             push (@{$linkage{$opt}}, $arg);
  760.             }
  761.             elsif ( ref($linkage{$opt}) eq 'HASH' ) {
  762.             print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n")
  763.                 if $debug;
  764.             $linkage{$opt}->{$key} = $arg;
  765.             }
  766.             elsif ( ref($linkage{$opt}) eq 'CODE' ) {
  767.             print STDERR ("=> &L{$opt}(\"$opt\", \"$arg\")\n")
  768.                 if $debug;
  769.             &{$linkage{$opt}}($opt, $arg);
  770.             }
  771.             else {
  772.             print STDERR ("Invalid REF type \"", ref($linkage{$opt}),
  773.                       "\" in linkage\n");
  774.             die ("Getopt::Long -- internal error!\n");
  775.             }
  776.         }
  777.         elsif ( $array ) {
  778.             if ( defined $userlinkage->{$opt} ) {
  779.             print STDERR ("=> push(\@{\$L{$opt}}, \"$arg\")\n")
  780.                 if $debug;
  781.             push (@{$userlinkage->{$opt}}, $arg);
  782.             }
  783.             else {
  784.             print STDERR ("=>\$L{$opt} = [\"$arg\"]\n")
  785.                 if $debug;
  786.             $userlinkage->{$opt} = [$arg];
  787.             }
  788.         }
  789.         elsif ( $hash ) {
  790.             if ( defined $userlinkage->{$opt} ) {
  791.             print STDERR ("=> \$L{$opt}->{$key} = \"$arg\"\n")
  792.                 if $debug;
  793.             $userlinkage->{$opt}->{$key} = $arg;
  794.             }
  795.             else {
  796.             print STDERR ("=>\$L{$opt} = {$key => \"$arg\"}\n")
  797.                 if $debug;
  798.             $userlinkage->{$opt} = {$key => $arg};
  799.             }
  800.         }
  801.         else {
  802.             print STDERR ("=>\$L{$opt} = \"$arg\"\n") if $debug;
  803.             $userlinkage->{$opt} = $arg;
  804.         }
  805.         }
  806.     }
  807.  
  808.     elsif ( $order == $PERMUTE ) {
  809.         my $cb;
  810.         if ( (defined ($cb = $linkage{'<>'})) ) {
  811.         &$cb($tryopt);
  812.         }
  813.         else {
  814.         print STDERR ("=> saving \"$tryopt\" ",
  815.                   "(not an option, may permute)\n") if $debug;
  816.         push (@ret, $tryopt);
  817.         }
  818.         next;
  819.     }
  820.  
  821.     else {
  822.         unshift (@ARGV, $tryopt);
  823.         return ($error == 0);
  824.     }
  825.  
  826.     }
  827.  
  828.     if ( $order == $PERMUTE ) {
  829.     print STDERR ("=> restoring \"", join('" "', @ret), "\"\n")
  830.         if $debug && @ret > 0;
  831.     unshift (@ARGV, @ret) if @ret > 0;
  832.     }
  833.  
  834.     return ($error == 0);
  835. }
  836.  
  837. sub config (@) {
  838.     my (@options) = @_;
  839.     my $opt;
  840.     foreach $opt ( @options ) {
  841.     my $try = lc ($opt);
  842.     my $action = 1;
  843.     if ( $try =~ /^no_?/ ) {
  844.         $action = 0;
  845.         $try = $';
  846.     }
  847.     if ( $try eq 'default' or $try eq 'defaults' ) {
  848.         &$config_defaults () if $action;
  849.     }
  850.     elsif ( $try eq 'auto_abbrev' or $try eq 'autoabbrev' ) {
  851.         $autoabbrev = $action;
  852.     }
  853.     elsif ( $try eq 'getopt_compat' ) {
  854.         $getopt_compat = $action;
  855.     }
  856.     elsif ( $try eq 'ignorecase' or $try eq 'ignore_case' ) {
  857.         $ignorecase = $action;
  858.     }
  859.     elsif ( $try eq 'ignore_case_always' ) {
  860.         $ignorecase = $action ? 2 : 0;
  861.     }
  862.     elsif ( $try eq 'bundling' ) {
  863.         $bundling = $action;
  864.     }
  865.     elsif ( $try eq 'bundling_override' ) {
  866.         $bundling = $action ? 2 : 0;
  867.     }
  868.     elsif ( $try eq 'require_order' ) {
  869.         $order = $action ? $REQUIRE_ORDER : $PERMUTE;
  870.     }
  871.     elsif ( $try eq 'permute' ) {
  872.         $order = $action ? $PERMUTE : $REQUIRE_ORDER;
  873.     }
  874.     elsif ( $try eq 'pass_through' or $try eq 'passthrough' ) {
  875.         $passthrough = $action;
  876.     }
  877.     elsif ( $try eq 'debug' ) {
  878.         $debug = $action;
  879.     }
  880.     else {
  881.         $Carp::CarpLevel = 1;
  882.         Carp::croak("Getopt::Long: unknown config parameter \"$opt\"")
  883.     }
  884.     }
  885. }
  886.  
  887. sub require_version {
  888.     no strict;
  889.     my ($self, $wanted) = @_;
  890.     my $pkg = ref $self || $self;
  891.     my $version = $ {"${pkg}::VERSION"} || "(undef)";
  892.  
  893.     $wanted .= '.0' unless $wanted =~ /\./;
  894.     $wanted = $1 * 1000 + $2 if $wanted =~ /^(\d+)\.(\d+)$/;
  895.     $version = $1 * 1000 + $2 if $version =~ /^(\d+)\.(\d+)$/;
  896.     if ( $version < $wanted ) {
  897.     $version =~ s/^(\d+)(\d\d\d)$/$1.'.'.(0+$2)/e;
  898.     $wanted =~ s/^(\d+)(\d\d\d)$/$1.'.'.(0+$2)/e;
  899.     $Carp::CarpLevel = 1;
  900.     Carp::croak("$pkg $wanted required--this is only version $version")
  901.     }
  902.     $version;
  903. }
  904.  
  905.  
  906. $find_option = sub {
  907.  
  908.     return 0 unless $opt =~ /^$genprefix/;
  909.  
  910.     $opt = $';
  911.     my ($starter) = $&;
  912.  
  913.     my $optarg = undef;    # value supplied with --opt=value
  914.     my $rest = undef;    # remainder from unbundling
  915.  
  916.     if (($starter eq "--" || $getopt_compat)
  917.     && $opt =~ /^([^=]+)=/ ) {
  918.     $opt = $1;
  919.     $optarg = $';
  920.     print STDERR ("=> option \"", $opt, 
  921.               "\", optarg = \"$optarg\"\n") if $debug;
  922.     }
  923.  
  924.  
  925.     my $tryopt = $opt;        # option to try
  926.     my $optbl = \%opctl;    # table to look it up (long names)
  927.     my $type;
  928.  
  929.     if ( $bundling && $starter eq '-' ) {
  930.     $rest = substr ($tryopt, 1);
  931.     $tryopt = substr ($tryopt, 0, 1);
  932.     $tryopt = lc ($tryopt) if $ignorecase > 1;
  933.     print STDERR ("=> $starter$tryopt unbundled from ",
  934.               "$starter$tryopt$rest\n") if $debug;
  935.     $rest = undef unless $rest ne '';
  936.     $optbl = \%bopctl;    # look it up in the short names table
  937.  
  938.     if ( $bundling == 2 and
  939.          defined ($type = $opctl{$tryopt.$rest}) ) {
  940.         print STDERR ("=> $starter$tryopt rebundled to ",
  941.               "$starter$tryopt$rest\n") if $debug;
  942.         $tryopt .= $rest;
  943.         undef $rest;
  944.     }
  945.     } 
  946.  
  947.     elsif ( $autoabbrev ) {
  948.     $tryopt = $opt = lc ($opt) if $ignorecase;
  949.     my $pat = quotemeta ($opt);
  950.     my @hits = grep (/^$pat/, @opctl);
  951.     print STDERR ("=> ", scalar(@hits), " hits (@hits) with \"$pat\" ",
  952.               "out of ", scalar(@opctl), "\n") if $debug;
  953.  
  954.     unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) {
  955.         my %hit;
  956.         foreach ( @hits ) {
  957.         $_ = $aliases{$_} if defined $aliases{$_};
  958.         $hit{$_} = 1;
  959.         }
  960.         unless ( keys(%hit) == 1 ) {
  961.         return 0 if $passthrough;
  962.         print STDERR ("Option ", $opt, " is ambiguous (",
  963.                   join(", ", @hits), ")\n");
  964.         $error++;
  965.         undef $opt;
  966.         return 1;
  967.         }
  968.         @hits = keys(%hit);
  969.     }
  970.  
  971.     if ( @hits == 1 && $hits[0] ne $opt ) {
  972.         $tryopt = $hits[0];
  973.         $tryopt = lc ($tryopt) if $ignorecase;
  974.         print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n")
  975.         if $debug;
  976.     }
  977.     }
  978.  
  979.     elsif ( $ignorecase ) {
  980.     $tryopt = lc ($opt);
  981.     }
  982.  
  983.     $type = $optbl->{$tryopt} unless defined $type;
  984.     unless  ( defined $type ) {
  985.     return 0 if $passthrough;
  986.     warn ("Unknown option: ", $opt, "\n");
  987.     $error++;
  988.     return 1;
  989.     }
  990.     $opt = $tryopt;
  991.     print STDERR ("=> found \"$type\" for ", $opt, "\n") if $debug;
  992.  
  993.  
  994.     if ( $type eq '' || $type eq '!' ) {
  995.     if ( defined $optarg ) {
  996.         return 0 if $passthrough;
  997.         print STDERR ("Option ", $opt, " does not take an argument\n");
  998.         $error++;
  999.         undef $opt;
  1000.     }
  1001.     elsif ( $type eq '' ) {
  1002.         $arg = 1;        # supply explicit value
  1003.     }
  1004.     else {
  1005.         substr ($opt, 0, 2) = ''; # strip NO prefix
  1006.         $arg = 0;        # supply explicit value
  1007.     }
  1008.     unshift (@ARGV, $starter.$rest) if defined $rest;
  1009.     return 1;
  1010.     }
  1011.  
  1012.     my $mand;
  1013.     ($mand, $type, $array, $hash) = $type =~ /^(.)(.)(@?)(%?)$/;
  1014.  
  1015.     if ( defined $optarg ? ($optarg eq '') 
  1016.      : !(defined $rest || @ARGV > 0) ) {
  1017.     if ( $mand eq "=" ) {
  1018.         return 0 if $passthrough;
  1019.         print STDERR ("Option ", $opt, " requires an argument\n");
  1020.         $error++;
  1021.         undef $opt;
  1022.     }
  1023.     if ( $mand eq ":" ) {
  1024.         $arg = $type eq "s" ? '' : 0;
  1025.     }
  1026.     return 1;
  1027.     }
  1028.  
  1029.     $arg = (defined $rest ? $rest
  1030.         : (defined $optarg ? $optarg : shift (@ARGV)));
  1031.  
  1032.     $key = undef;
  1033.     if ($hash && defined $arg) {
  1034.     ($key, $arg) = ($arg =~ /=/o) ? ($`, $') : ($arg, 1);
  1035.     }
  1036.  
  1037.  
  1038.     if ( $type eq "s" ) {    # string
  1039.     return 1 if $mand eq "=";
  1040.  
  1041.     return 1 if defined $optarg || defined $rest;
  1042.     return 1 if $arg eq "-"; # ??
  1043.  
  1044.     if ($arg eq $argend ||
  1045.         $arg =~ /^$genprefix.+/) {
  1046.         unshift (@ARGV, $arg);
  1047.         $arg = '';
  1048.     }
  1049.     }
  1050.  
  1051.     elsif ( $type eq "n" || $type eq "i" ) { # numeric/integer
  1052.     if ( $arg !~ /^-?[0-9]+$/ ) {
  1053.         if ( defined $optarg || $mand eq "=" ) {
  1054.         return 0 if $passthrough;
  1055.         print STDERR ("Value \"", $arg, "\" invalid for option ",
  1056.                   $opt, " (number expected)\n");
  1057.         $error++;
  1058.         undef $opt;
  1059.         unshift (@ARGV, $starter.$rest) if defined $rest;
  1060.         }
  1061.         else {
  1062.         unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
  1063.         $arg = 0;
  1064.         }
  1065.     }
  1066.     }
  1067.  
  1068.     elsif ( $type eq "f" ) { # real number, int is also ok
  1069.     if ( $arg !~ /^-?[0-9.]+([eE]-?[0-9]+)?$/ ) {
  1070.         if ( defined $optarg || $mand eq "=" ) {
  1071.         return 0 if  $passthrough;
  1072.         print STDERR ("Value \"", $arg, "\" invalid for option ",
  1073.                   $opt, " (real number expected)\n");
  1074.         $error++;
  1075.         undef $opt;
  1076.         unshift (@ARGV, $starter.$rest) if defined $rest;
  1077.         }
  1078.         else {
  1079.         unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
  1080.         $arg = 0.0;
  1081.         }
  1082.     }
  1083.     }
  1084.     else {
  1085.     die ("GetOpt::Long internal error (Can't happen)\n");
  1086.     }
  1087.     return 1;
  1088. };
  1089.  
  1090. $config_defaults = sub {
  1091.     if ( defined $ENV{"POSIXLY_CORRECT"} ) {
  1092.     $gen_prefix = "(--|-)";
  1093.     $autoabbrev = 0;        # no automatic abbrev of options
  1094.     $bundling = 0;            # no bundling of single letter switches
  1095.     $getopt_compat = 0;        # disallow '+' to start options
  1096.     $order = $REQUIRE_ORDER;
  1097.     }
  1098.     else {
  1099.     $gen_prefix = "(--|-|\\+)";
  1100.     $autoabbrev = 1;        # automatic abbrev of options
  1101.     $bundling = 0;            # bundling off by default
  1102.     $getopt_compat = 1;        # allow '+' to start options
  1103.     $order = $PERMUTE;
  1104.     }
  1105.     $debug = 0;            # for debugging
  1106.     $error = 0;            # error tally
  1107.     $ignorecase = 1;        # ignore case when matching options
  1108.     $passthrough = 0;        # leave unrecognized options alone
  1109. };
  1110.  
  1111.  
  1112. ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) = (0..2);
  1113. ($major_version, $minor_version) = $VERSION =~ /^(\d+)\.(\d+)/;
  1114.  
  1115. &$config_defaults ();
  1116.  
  1117.  
  1118. 1;
  1119.